package cn.iocoder.yudao.module.bpm.service.tbprocess;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.bpm.dal.mysql.tbprocess.TbProcessPageMapper;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.bpm.controller.admin.tbprocess.vo.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.tbprocess.TbProcessDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.bpm.convert.tbprocess.TbProcessConvert;
import cn.iocoder.yudao.module.bpm.dal.mysql.tbprocess.TbProcessMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*;

/**
 * 图斑和流程实例关联 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class TbProcessServiceImpl implements TbProcessService {

    @Resource
    private TbProcessMapper tbProcessMapper;
    @Resource
    private TbProcessPageMapper tbProcessPageMapper;

    @Override
    public Long createTbProcess(TbProcessCreateReqVO createReqVO) {
        // 插入
        TbProcessDO tbProcess = TbProcessConvert.INSTANCE.convert(createReqVO);
        tbProcessMapper.insert(tbProcess);
        // 返回
        return tbProcess.getId();
    }

    @Override
    public Long createMutiTbProcess(List<TbProcessCreateReqVO> createReqVO) {
        for(TbProcessCreateReqVO tp:createReqVO){
            // 插入
            TbProcessDO tbProcess = TbProcessConvert.INSTANCE.convert(tp);
            tbProcessMapper.insert(tbProcess);
        }
        return 1L;
    }

    @Override
    public void updateTbProcess(TbProcessUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateTbProcessExists(updateReqVO.getId());
        // 更新
        TbProcessDO updateObj = TbProcessConvert.INSTANCE.convert(updateReqVO);
        tbProcessMapper.updateById(updateObj);
    }

    @Override
    public void deleteTbProcess(Long id) {
        // 校验存在
        this.validateTbProcessExists(id);
        // 删除
        tbProcessMapper.deleteById(id);
    }

    private void validateTbProcessExists(Long id) {
        if (tbProcessMapper.selectById(id) == null) {
            throw exception(TB_PROCESS_NOT_EXISTS);
        }
    }

    @Override
    public TbProcessDO getTbProcess(Long id) {
        return tbProcessMapper.selectById(id);
    }

    @Override
    public List<TbProcessDO> getTbProcessList(Collection<Long> ids) {
        return tbProcessMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<TbProcessDO> getTbProcessPage(TbProcessPageReqVO pageReqVO) {
        return tbProcessMapper.selectPage(pageReqVO);
    }

    @Override
    public List<TbProcessDO> getTbProcessList(TbProcessExportReqVO exportReqVO) {
        return tbProcessMapper.selectList(exportReqVO);
    }

    @Override
    public Page<TbProcessManageVO> selectPageGroup(IPage<TbProcessManageVO> iPage, TbProcessManageVO pageVO) {
        LambdaQueryWrapperX<TbProcessManageVO> queryWrapper=new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(TbProcessManageVO::getId, pageVO.getId())
                .eqIfPresent(TbProcessManageVO::getProcessInstanceId, pageVO.getProcessInstanceId());
        return tbProcessPageMapper.selectPageGroup(iPage,queryWrapper);
    }

}
